﻿#include <iostream>
#include <thread>
#include <string>
using namespace std;
struct func
{
	int& i;
	func(int& i_) :i(i_) {}
	void operator()()
	{
		for (unsigned j = 0; j < 1000; j++)
		{
			i++;
		}
	}
};

class thread_guard
{
private:
	thread& t;
public:
	explicit thread_guard(thread& t_) : t(t_) {}
	~thread_guard()
	{
		if (t.joinable())
			t.join();
	}
	thread_guard(thread_guard const&) = delete;
	thread_guard& operator=(thread_guard const&) = delete;
};

void f()
{
	int some_local_state = 0;
	func my_func(some_local_state);
	thread t(my_func);
	thread_guard g(t);
}

void test()
{
	for (int i = 0; i < 2100; i++)
	{
		cout << i << endl;
	}
}


int main()
{
	thread t(test);
	cout << "hello world" << endl;
	for (int i = 0; i < 500; i++)
		cout << "1";
	cout << endl;
	t.join();
	cout << "end" << endl;
	return 0;
}